package org.gbif.portal.filter;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

class UrlRewriteRequest extends HttpServletRequestWrapper{
	private Map<String,String> params=new HashMap<String,String>();
	private String requestURI;
	private String servletPath;
	
	public UrlRewriteRequest(HttpServletRequest request, String url, Map<String,String> additionalParameter) {
		super(request);
		this.requestURI=url;
		if (additionalParameter!=null){
			params=additionalParameter;
		}else{
			params=new HashMap<String,String>();
		}
		// only use first value of each parameter:
		Enumeration<String> iter = super.getParameterNames(); 
		while(iter.hasMoreElements()){
			String p = iter.nextElement();
	        params.put(p,super.getParameter(p));
		}
	}

	@Override
	public String getParameter(String name) {
        return params.get(name);
	}

	@Override
	public Enumeration<String> getParameterNames() {
        return Collections.enumeration(params.keySet());
    }
	
	@Override
	public Map getParameterMap() {
        return params;
	}

	@Override
	public String[] getParameterValues(String s) {
        if (params.containsKey(s)) {
            return new String[]{params.get(s)};
        }else{
            return new String[]{};
        }
    }
	
	@Override
	public String getContextPath() {
		String x=super.getContextPath();
//		System.out.println("req.getContextPath():"+x);
		return x;
	}

	@Override
	public String getPathInfo() {
		String x=super.getPathInfo();
//		System.out.println("req.getPathInfo():"+x);
		return x;
	}

	@Override
	public String getPathTranslated() {
		String x=super.getPathTranslated();
//		System.out.println("req.getPathTranslated():"+x);
		return x;
	}

	@Override
	public String getQueryString() {
		String x=super.getQueryString();
//		System.out.println("req.getQueryString():"+x);
		String newQS="";
		boolean amp=false;
		if (x==null){
			x="";
		}else if (x.trim().length()>0){
			amp=true;
		}
		for (String p : params.keySet()){
			if (amp){
				newQS+="&";	
			}else{
				amp=true;
			}
			newQS+=p+"="+params.get(p);
		}
		x=x+newQS;
		System.out.println(" NEW:"+x);
		return x;
	}

	@Override
	public String getRequestURI() {
//		String x=super.getRequestURI();
//		System.out.println("req.getRequestURI():"+x);
//		System.out.println("req.getRequestURI():"+requestURI);
		return requestURI;
	}

	@Override
	public StringBuffer getRequestURL() {
//		StringBuffer x=super.getRequestURL();
//		System.out.println("req.getRequestURL():"+x);
		StringBuffer x=new StringBuffer(requestURI);
//		System.out.println("req.getRequestURL() NEW:"+x.toString());
		return x;
	}

	@Override
	public String getServletPath() {
//		String x=super.getServletPath();
//		System.out.println("req.getServletPath():"+x);
//		System.out.println("req.getServletPath() NEW:"+requestURI);
		return requestURI;
	}
	
}
